package
{
	import away3d.cameras.Camera3D;
	
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.ui.Keyboard;

	public class CameraInputHandler implements IInputHandler
	{
		private var m_camera:Camera3D;
		private var m_lastPosition:Point;
		
		private var m_mouseDown:Boolean = false;
		
		private var m_shiftDown:Boolean = false;
		
		public function CameraInputHandler(camera:Camera3D)
		{
			m_camera = camera;
			m_lastPosition = new Point(0,0);
		}
		
		private function getMouseDelta(me:MouseEvent):Point{
			var curPoint:Point = new Point(me.stageX, me.stageY);
			var delta:Point = m_lastPosition.subtract(curPoint);
			m_lastPosition = curPoint;
			
			return delta;
		}
		
		public function onMouseMove(me:MouseEvent):void{
			var delta:Point = getMouseDelta(me);
			if(isDragging()){
				if(m_shiftDown){
					m_camera.rotationX += delta.y/Math.PI;
					m_camera.rotationY += delta.x/Math.PI;
				} else {
					
					m_camera.x += delta.x*4;
					m_camera.y -= delta.y*4;
				}
				
			}
		}
		
		public function onMouseDown(me:MouseEvent):void{
			m_mouseDown = true;
			m_lastPosition = new Point(me.stageX, me.stageY);
		}
		
		public function onMouseUp(me:MouseEvent):void{
			m_mouseDown = false;
		}
		
		public function onMouseWheel(me:MouseEvent):void{
			m_camera.zoom += me.delta*.05;
		}		
		
		private function isDragging():Boolean{
			return m_mouseDown;
		}
		
		public function onKeyDown(ke:KeyboardEvent):void{
			if(ke.shiftKey){
				m_shiftDown = ke.shiftKey
			}
		}
		
		public function onKeyUp(ke:KeyboardEvent):void{
			m_shiftDown = false;	
		}			
	}
}